package com.itheima.tliasmanagent.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tliasmanagent.mapper.EmpExprMapper;
import com.itheima.tliasmanagent.mapper.EmpMapper;
import com.itheima.tliasmanagent.pojo.*;
import com.itheima.tliasmanagent.service.EmpLogService;
import com.itheima.tliasmanagent.service.EmpService;
import com.itheima.tliasmanagent.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;
/*    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1.先获取总条数
        Long total=empMapper.count();

        //2.获取列表数据
        // 分页的sql语句   limit 起始索引,显示条数
        Integer startIndex = (page-1)*pageSize;
        List list=empMapper.list(startIndex,pageSize);

        return new PageBean(total,list);
    }*/

    // 插件的代码写法
    @Override
    public PageBean page(EmpQueryParam empQueryParam) {
     //1. 设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());

        //2.调用方法查询数据
        Page<Emp> empPage= empMapper.list( empQueryParam);

        return new PageBean(empPage.getTotal(),empPage.getResult());
    }


    @Autowired
    private EmpLogService empLogService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        try {
            //1. 新增员工表数据
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);

            //int i=10/0;// 产生了一个异常对象
          /*  if(true){
            throw  new Exception("我异常了");
            }*/
            //2.新增 员工工作经历表
            Integer empId=emp.getId();
            List<EmpExpr> exprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){
                exprList.forEach(empExpr -> { empExpr.setEmpId(empId);   });
                empExprMapper.insertBatch(exprList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
        empLogService.insertLog(new EmpLog(null,LocalDateTime.now(),emp.toString()));
        }

    }

    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        // 删除员工信息
        empMapper.deleteByIds(ids);

        // 删除员工工作经历信息
        empExprMapper.deleteByEmpId(ids);

    }

    @Override
    public Emp getInfo(Integer id) {
     Emp emp= empMapper.getById(id);

        return emp;
    }

    @Transactional
    @Override
    public void update(Emp emp) {
        //1. 根据ID修改员工信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

        //2. 修改员工工作经历信息 ---->先删除 后新增
        //2.1 先删除
        empExprMapper.deleteByEmpId(Arrays.asList(emp.getId()));
        //2.2 后新增

        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(expr -> expr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }

    }

    @Override
    public LoginInfo login(Emp emp) {
       Emp empLogin= empMapper.getUsernameAndPassword(emp);
       if(empLogin != null){
           Map<String, Object> claim = new HashMap<>();
           claim.put("id",empLogin.getId());
           claim.put("username",empLogin.getUsername());
           String jwt = JwtUtils.generateJwt(claim);
           return  new LoginInfo(empLogin.getId(),empLogin.getUsername(),empLogin.getName(),jwt);

       }
        return null;
    }
}
